import { Root, Type, Message, common, load } from 'protobufjs'
const anyProto = Root.fromJSON(common["google/protobuf/any.proto"])
const emptyProto = Root.fromJSON(common["google/protobuf/empty.proto"])
const wrappersProto = Root.fromJSON(common["google/protobuf/wrappers.proto"])
export const Any = anyProto.lookupType("google.protobuf.Any")
export const Empty = emptyProto.lookupType("google.protobuf.Empty")
export const BoolValue = wrappersProto.lookupType("google.protobuf.BoolValue")
export const BytesValue = wrappersProto.lookupType("google.protobuf.BytesValue")
export const FloatValue = wrappersProto.lookupType("google.protobuf.FloatValue")
export const Int32Value = wrappersProto.lookupType("google.protobuf.Int32Value")
export const Int64Value = wrappersProto.lookupType("google.protobuf.Int64Value")
export const StringValue = wrappersProto.lookupType("google.protobuf.StringValue")
let elegantProto: Root = null
export var Interaction: Type = null
load('elegant.proto').then(proto => {
    elegantProto = proto
    Interaction = elegantProto.lookupType("elegant.Interaction")
})
export function fromAny(val: Message, cls: Type) {
    try {
        if (cls == Interaction)
            return cls.decode(val["value"])
        if (cls == Empty)
            return null
        return cls.decode(val["value"])["value"]
    } catch (e) {
        console.log(e)
    }
}
export function toAny(val: number | string | boolean | Uint8Array, cls: Type): Message {
    const anyVal = (cls == Empty ? Empty.encode({}) : cls.encode({ value: val })).finish()
    return Any.fromObject({
        type_url: "type.googleapis.com/" + cls.fullName.slice(1),
        value: anyVal
    })
}